package net.dgg.rz.common.service;

import net.dgg.iboss.base.service.CmsService;
import net.dgg.iboss.base.service.OrfApiService;
import net.dgg.rz.common.utils.ValidateUtils;
import net.dgg.rz.production.exception.ProductOrderExcption;
import net.dgg.tmd.foundation.platform.org.service.OrganizationManager;
import net.dgg.tmd.foundation.platform.role.service.RoleService;
import net.dgg.tmd.foundation.platform.treebook.entity.TreeBook;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import net.dgg.rz.common.base.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by admin on 2018/4/28.
 */
@Service
public class CommonMangerExtService extends BaseService {
    @Resource
    private CmsService cmsService;
    @Resource
    private OrganizationManager organizationManager;

    /**
     * 根据分类编码查询下级
     *
     * @param code  code
     * @param type  1：根据分类编码查询下级第一级分类（用于Select选择）
     *              2：根据分类编码查询所有下级分类（用于Tree选择无父级的）
     *              3：根据分类编码查询所有下级分类包含自己（用于Tree选择有父级的）
     *              4：根据分类编码查询所有下级分类（用于Tree选择无父级的）,查询包含禁用的字典
     * @param level 查询深度级数
     * @return
     */
    public List<TreeBook> queryTreeBookListByCode(String code, Integer type, Integer level) {
        //TODO
        List<TreeBook> list = new ArrayList<>();
        List<Map> datas = cmsService.getTreeBookList(code, 1, type, level);
        if (null != datas) {
            for (Map data : datas) {
                TreeBook book = new TreeBook();
                book.setId(Long.valueOf(data.get("id").toString()));
                book.setPid(Long.valueOf(data.get("pid").toString()));
                book.setCode(data.get("code") == null ? "" : data.get("code").toString());
                book.setName(data.get("name") == null ? "" : data.get("name").toString());
                book.setPcode(data.get("pcode") == null ? "" : data.get("pcode").toString());
                book.setExt1(data.get("ext1") == null ? "" : data.get("ext1").toString());
                book.setDescription(data.get("description") == null ? "" : data.get("description").toString());
                list.add(book);
            }
        }
        return list;
    }

    /**
     * @Title:
     * @Description: TODO 字典编码单个翻译
     * @param:
     * @return:
     * @throw:
     * @author: 代杨
     * @creat_date: 2018/6/22
     **/
    public Map getTreeBookObj(String code) {
        Map reMap = cmsService.getTreeBookObj(code);
        return reMap;
    }

    /**
     * 通过父级部门的id，获取本级及下级所有部门的id集合
     *
     * @param orgId
     * @return
     */
    public List<Long> getChildOrganizationList(Long orgId) {
        List<Long> orgs = new ArrayList<>();
        orgs.add(orgId);
        orgs.addAll(organizationManager.findOrgIdByAncestorId(orgId));
        return orgs;
    }

    /**
     * 分页查询用户列表
     *
     * @param param
     * @return
     */
    public List<Map> searchUserPage(Map param) {
        try {
            Map result = cmsService.findPageUsers(param);
            if (result.isEmpty()) {
                param.put("count", 0);
                return new ArrayList<>(0);
            }
            param.put("count", result.get("recordsTotal"));
            return (List<Map>) result.get("data");
        } catch (Exception e) {
            param.put("count", 0);
            return new ArrayList<>(0);
        }
    }

    /**
     * @Title:
     * @Description: TODO 通过id查询用户
     * @param:
     * @return:
     * @throw:
     * @author: 代杨
     * @creat_date: 2018/5/17
     **/
    public Map searchUserById(Long id) {
        return cmsService.findUser(id);
    }

    /**
     * @Title:
     * @Description: TODO 查询部门列表接口
     * @param:
     * @return:
     * @throw:
     * @author: 代杨
     * @creat_date: 2018/5/11
     **/
    public List<Map> findOrgs(Map params) {
        List<Map> list = cmsService.findOrgs(params);
        return list;
    }

    /**
     * @Title:
     * @Description: TODO 查询部门接口
     * @param:
     * @return:
     * @throw:
     * @author: 代杨
     * @creat_date: 2018/5/11
     **/
    public Map findOrg(Long id) {
        Map map = cmsService.findOrg(id);
        return map;
    }

    /**
     * @Title:
     * @Description: TODO 查询当前部门的事业部
     * @param:
     * @return:
     * @throw:
     * @author: 代杨
     * @creat_date: 2018/5/11
     **/
    public Map findBusDeptById(Long deptId) {
        Map map = cmsService.findOrgParent(deptId, "ORG_LEVEL_1");
        return map;
    }

}
